"""
【访问入加操作】
访问者模式(Visitor Pattern)
定义了一种新的操作方式，可在不改变一个对象的类的前提下，向该对象添加新的操作。这个模式通常用于需对一个复杂的对象结构进行处理的场景。

一个原类，对应两个实现类，一个新操作抽象类，两个操作实现类去按对应传入的实现类调用方法
"""
from abc import ABC, abstractmethod


class Visitor(ABC):
    @abstractmethod
    def visit_element_a(self, element_a):
        pass

    @abstractmethod
    def visit_element_b(self, element_b):
        pass


class Element(ABC):
    @abstractmethod
    def accept(self, visitor):
        pass


class ConcreteElementA(Element):  # 新操作
    def accept(self, visitor):
        visitor.visit_element_a(self)


class ConcreteElementB(Element):  # 新操作
    def accept(self, visitor):
        visitor.visit_element_b(self)


class ConcreteVisitor1(Visitor):
    def visit_element_a(self, element_a):
        print("ConcreteVisitor1 visit ConcreteElementA")

    def visit_element_b(self, element_b):
        print("ConcreteVisitor1 visit ConcreteElementB")


class ConcreteVisitor2(Visitor):
    def visit_element_a(self, element_a):
        print("ConcreteVisitor2 visit ConcreteElementA")

    def visit_element_b(self, element_b):
        print("ConcreteVisitor2 visit ConcreteElementB")


elements = [ConcreteElementA(), ConcreteElementB()]
visitors = [ConcreteVisitor1(), ConcreteVisitor2()]

for element in elements:
    for visitor in visitors:
        element.accept(visitor)
'''
ConcreteVisitor1 visit ConcreteElementA
ConcreteVisitor2 visit ConcreteElementA
ConcreteVisitor1 visit ConcreteElementB
ConcreteVisitor2 visit ConcreteElementB
'''
